//
//  LcbFrameAutoScale.m
//  lcb
//
//  Created by lcb on 2017/3/15.
//  Copyright © 2017年 lcb. All rights reserved.
//

#import "LcbFrameAutoScale.h"
#import "LcbFrame.h"
@interface LcbFrameAutoScale ()
@property (nonatomic ,assign)float autoSizeScaleX;/**< 宽度缩放的比例  */
@property (nonatomic ,assign)float autoSizeScaleY;/**< 高度缩放的比例  */
@end

@implementation LcbFrameAutoScale
static LcbFrameAutoScale *SINGLETON = nil;

static bool isFirstAccess = YES;

#pragma mark - Public Method

+ (instancetype)sharedLcbFrameAutoScale{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        isFirstAccess = NO;
        SINGLETON = [[super allocWithZone:NULL] init];
        
    });
    
    return SINGLETON;
}
- (instancetype) init
{
    if(SINGLETON){
        return SINGLETON;
    }
    if (isFirstAccess) {
        [self doesNotRecognizeSelector:_cmd];
    }
    [self AutoSizeScale];
    self = [super init];
    return self;
}

#pragma mark - Life Cycle

+ (instancetype) allocWithZone:(NSZone *)zone
{
    return [self sharedLcbFrameAutoScale];
}

#pragma mark ----storLcborad Xib AutoLayouts
/**
 *   storLcboard中得view自动适配
 *  @param storLcboradClassName 需要适配的storLcborad类名.
 */
+ (void)storLcboradLcbAutoLayoutsWithstorLcboradClassName:(NSString *)storLcboradClassName
{
    [self scaleAutoLayouts: [[UIViewController alloc] initWithNibName:storLcboradClassName bundle:nil].view ];
}

/**
 *   Xib中得view自动适配
 *  @param NibClassName 需要适配的xib类名
 */
+ (void)xibLcbAutoLayoutsWithNibClassName:(NSString *)NibClassName
{
    [self scaleAutoLayouts:[[[NSBundle mainBundle] loadNibNamed: NibClassName owner:nil options:nil]firstObject]];
}
/**
 *可视化的缩放适配
 *  @param LayoutsBaseView 需要适配的控件添加的父类view
 */
+ (void)scaleAutoLayouts:(UIView *)LayoutsBaseView{
    for (UIView *viewLcb in LayoutsBaseView.subviews) {
        viewLcb.frame = [LcbFrameAutoScale CGLcbMakeX:viewLcb.frame.origin.x Y:viewLcb.frame.origin.y width:viewLcb.frame.size.width height:viewLcb.frame.size.height];
        if (viewLcb.subviews.count) {
            [self scaleAutoLayouts:LayoutsBaseView];
        }
    }
}


#pragma mark -CGRect
/**
 setting a view Frame With the UIfigure  number  all value will be size to fit UIScreen
 *  @return 全部对应数值都将按照比例缩放返回一个进过处理缩放比例的frame.
 */
+ (CGRect)CGLcbMakeX:(CGFloat) x Y:(CGFloat) y width:(CGFloat) width height:(CGFloat) height{
    return CGLcbMake(x, y, width, height);
}
/**
 setting a view Frame With the UIfigure number special CGRectGetY
 全部对应数值都将按照比例缩放而Y参数除外的frame.eg: 获取上个控件的Y,不可以再次缩放.
 */
+ (CGRect)CGLcbMakeX:(CGFloat) x CGRectGetY:(CGFloat) GetY width:(CGFloat) width height:(CGFloat) height{
    CGFloat CGRectGetY = [LcbFrameAutoScale sharedLcbFrameAutoScale].autoSizeScaleY;
    return CGLcbMake(x, GetY / CGRectGetY, width, height);
}
/**
 返回正常处理通过CGRectGetX方式的frame(其他均正常)
 */
+ (CGRect)CGLcbMakeGetX:(CGFloat)GetX Y:(CGFloat) Y width:(CGFloat) width height:(CGFloat) height{
    CGFloat CGRectGetX = [LcbFrameAutoScale sharedLcbFrameAutoScale].autoSizeScaleX;
    return CGLcbMake(GetX / CGRectGetX, Y, width, height);
}
/**
 setting a view Frame With the UIfigure number special height eg: 64  always 64 Value
 比如导航栏的高度,一直不变,或者设置固定的高度,可以使用.
 */
+ (CGRect)CGLcbMakeX:(CGFloat) x Y:(CGFloat) y  width:(CGFloat) width heightAllSame:(CGFloat) heightAllSame{
    CGFloat heightValue_Lcb = [LcbFrameAutoScale sharedLcbFrameAutoScale].autoSizeScaleY;
    return CGLcbMake(x, y, width,heightAllSame / heightValue_Lcb);
}

/**
 返回一个全屏幕的 frame
 */
+ (CGRect)CGLcbfullScreen{
    return CGLcbMake(0, 0, RealUISrceenWidth, RealUISrceenHight);
}
#pragma mark CGPoint
+ (CGPoint)CGLcbPointMakeX:(CGFloat) x Y:(CGFloat) y{
    return CGPointLcbMake(x, y);
}
#pragma mark CGsize
+ (CGSize)CGSizeLcbMakeWidth:(CGFloat) widthLcb hight:(CGFloat) hightLcb{
    return CGSizeLcbMake(widthLcb, hightLcb);
}
+ (CGSize)CGSizeLcbMakeWH:(CGFloat)WH{
    return CGSizeLcbMake(WH, WH);
}
+ (CGSize)CGSizeLcbMakeMainScreenSize{
    return CGSizeLcbMake( RealUISrceenWidth, RealUISrceenHight);
}

#pragma mark 重写CGRectMake方法
/**
 重写CGRectMake 方法
 */
CG_INLINE CGRect
CGLcbMake(CGFloat x, CGFloat y, CGFloat width, CGFloat height)
{
    
    LcbFrameAutoScale *Lcb = [LcbFrameAutoScale sharedLcbFrameAutoScale];
    CGRect rect;
    rect.origin.x = x *Lcb.autoSizeScaleX;
    rect.origin.y = y * Lcb.autoSizeScaleY;
    rect.size.width = width * Lcb.autoSizeScaleX;
    rect.size.height = height * Lcb.autoSizeScaleY;
    return rect;
}
/**
 重写CGPoint 方法
 */
CG_INLINE CGPoint
CGPointLcbMake(CGFloat x, CGFloat y)
{
    LcbFrameAutoScale *Lcb = [LcbFrameAutoScale sharedLcbFrameAutoScale];
    CGPoint pointLcb;
    pointLcb.x = x * Lcb.autoSizeScaleX;
    pointLcb.y = y * Lcb.autoSizeScaleY;
    return pointLcb;
}
/**
 重写CGSize 方法
 */
CG_INLINE CGSize
CGSizeLcbMake(CGFloat width, CGFloat height)
{
    LcbFrameAutoScale *Lcb = [LcbFrameAutoScale sharedLcbFrameAutoScale];
    CGSize sizeLcb;
    sizeLcb.width = width* Lcb.autoSizeScaleX;
    sizeLcb.height = height* Lcb.autoSizeScaleY;
    return sizeLcb;
}

#pragma mark------所有设置中只计算一次缩放比例
- (void)AutoSizeScale{
    _autoSizeScaleX = ScreenWidthLcb/RealUISrceenWidth;
    _autoSizeScaleY = ScreenHightLcb/RealUISrceenHight;
}
@end
